<?php

namespace App\Admin\Controllers;

use App\Http\Traits\GlobalConstant;
use App\Models\Mutual;
use App\Models\Recommend;
use Encore\Admin\Form;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Support\Facades\Log;

class MutualController extends BaseController
{


    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('课程交互详情')
            ->description(' ')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑课程交互')
            ->description(' ')
            ->body($this->form($id)->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新增课程交互')
            ->description('新增课程交互')
            ->body($this->form());
    }


    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Mutual::findOrFail($id));

        $show->panel()
            ->tools(function ($tools) {
                $tools->disableDelete();
            });
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($id = 0)
    {
        $recommendId = request('r_id') ?? 0;
        $form        = new Form(new Mutual());
        if (!empty($id)) {
            $recommendId = Mutual::where('id', $id)->value('recommend_id');
        }
        $form->hidden('id', 'id');
        if (empty($recommendId)) {
            $form->select('recommend_id', '视频')->options(Recommend::where('status', 1)->pluck('title as text', 'id'))->required();
        } else {
            $form->hidden('recommend_id', '视频')->default($recommendId);
        }
        $form->text('question', '问题')->required()->attribute(['autocomplete' => 'off']);
        $form->number('point', '交互起点(秒)')->required()->min(1)->default(1)->help('视频播放到几秒出现');
        $form->number('score', '答对积分')->required()->min(0)->default(0);
        $form->list('answer', '答案');
        $form->text('right_answer', '正确答案')->required();
        $form->radio('status', '状态')->options(GlobalConstant::_STATUS)->default(1);
        $form->tools(function (Form\Tools $tools) use ($recommendId) {
            // 去掉`删除`按钮
            $tools->disableDelete();
            $tools->disableView();
            $tools->disableList();
            $url  = '/admin/recommend-edit?r_id=' . $recommendId;
            $html = <<<HTML
<div class="btn-group pull-right grid-create-btn" style="margin-right: 10px">
    <a href="$url" class="btn btn-sm btn-success">
        <i class="fa fa-bars"></i>&nbsp;&nbsp;返回推荐视频详情
    </a>
</div>
HTML;
            $tools->add($html);
        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
            $footer->disableReset();
        });
        if ($id == 0) {
            $form->setAction('/admin/mutual/save');
        } else {
            $form->setAction('/admin/mutual/update');
        }

        return $form;
    }

    public function store()
    {
        $param = request()->input();
        $info  = Mutual::where('recommend_id', $param['recommend_id'])->where('point', $param['point'])->first();
        if ($info) {
            Log::error('当前时间点已存在');
            admin_error('当前时间点"' . $param['point'] . '"在当前视频已经存在');
            return back();
        }
        unset($param['_token']);
        unset($param['_previous_']);
        unset($param['s']);
        $param['answer'] = $param['answer']['values'];
        Mutual::create($param);
        return redirect('admin/recommend-edit?r_id=' . $param['recommend_id']);
    }

    public function update($id)
    {
        $param = request()->input();
        $info  = Mutual::where('recommend_id', $param['recommend_id'])->where('point', $param['point'])->where('id', '!=', $param['id'])->first();
        if ($info) {
            Log::error('当前时间点已存在2');
            admin_error('当前时间点"' . $param['point'] . '"在当前视频已经存在');
            return back();
        }
        try {
            unset($param['_token']);
            unset($param['_method']);
            unset($param['_previous_']);
            unset($param['s']);
            $param['answer'] = $param['answer']['values'];
            $detail          = Mutual::where('id', $param['id'])->first();
            $detail->fill($param);
            $detail->save();
            admin_success("操作成功");
            return redirect('admin/recommend-edit?r_id=' . $param['recommend_id']);
        } catch (\Exception $exception) {
            admin_error("操作失败,原因" . $exception->getMessage());
            return back();
        }
    }

}
